<template>
	<statusBarVue title="一键发布需求" :isShowBar="true" :isShowLeft="true" />
	<view class="add-demand-page" :style="pageHaveBarStyle">
		<uni-list :border="false">
			<uni-forms ref="baseForm" class="form" :modelValue="baseFormData" :rules="rules" label-position="top"
				label-width="160px" :border="true">

				<view class="add-demand-page-group">
					<uni-list-item>
						<template v-slot:body>
							<uni-forms-item label="我需要..." name="service_types" required>
								<view class="long-width">
									<uni-data-checkbox multiple class="checklist-tag" mode="tag"
										v-model="baseFormData.service_types" selectedColor="#22A366"
										selectedTextColor="#fff" :localdata="typeListArrObj"></uni-data-checkbox>
								</view>
							</uni-forms-item>
						</template>
					</uni-list-item>
				</view>

				<view class="add-demand-page-group">
					<uni-list-item>
						<template v-slot:body>
							<uni-forms-item label="详细需求" name="content" required>
								<view class="long-width">
									<uni-easyinput class="input textarea" type="textarea" maxlength="200"
										:placeholderStyle="placeholderStyle" v-model="baseFormData.content"
										placeholder="请描述具体施工内容，方便工友带齐工具，节省双方时间(200个字以内)" primaryColor="#22A366" />
								</view>
							</uni-forms-item>
						</template>
					</uni-list-item>
				</view>

				<view class="add-demand-page-group">
					<!-- <uni-list-item>
						<template v-slot:header>
							<uni-forms-item label="服务时间" name="at" required></uni-forms-item>
						</template>
						<template v-slot:body>
							<picker mode="date" :value="baseFormData.at" :end="baseFormData.deadline"
								@change="(e) => this.baseFormData.at = e.detail.value">
								<text :class="baseFormData.at ? 'content' : 'placeholder'">
									{{ baseFormData.at ? baseFormData.at : '' }}
								</text>
								<uni-icons type="forward" size="12"></uni-icons>
							</picker>
						</template>
					</uni-list-item> -->
					
					<uni-list-item>
						<template v-slot:header>
							<uni-forms-item label="时间？" name="atRange" required></uni-forms-item>
						</template>
						<template v-slot:body>
							<view class="end-time">
								<uni-datetime-picker :value="baseFormData.atRange" type="daterange" @change="handleDateRange">
									<text :class="baseFormData.atRange.length ? 'content' : 'placeholder'">
										{{ baseFormData.atRange.length ? 
										 ((baseFormData.atRange).join(' ~ '))
										 : '' }}
									</text>
									<uni-icons type="forward" size="12"></uni-icons>
								</uni-datetime-picker >
							</view>
						</template>
					</uni-list-item>
				</view>

				<view class="add-demand-page-group">

					<uni-list-item>
						<template v-slot:header>
							<uni-forms-item label="在哪里？" name="serv_addr" required></uni-forms-item>
						</template>
						<template v-slot:body>
							<picker :value="baseFormData.serv_addr" mode="region"
								@change="e => { this.baseFormData.serv_addr = e.detail.value; }">
								<text :class="(Array.isArray(baseFormData.serv_addr)) ? 'content' : 'placeholder'">
									{{ (Array.isArray(baseFormData.serv_addr)) ? (baseFormData.serv_addr).join(',') :
										'' }}
								</text>
								<uni-icons type="forward" size="12"></uni-icons>
							</picker>
						</template>
					</uni-list-item>

				</view>
			</uni-forms>
		</uni-list>
		<view class="submit">
			<button class="btn-submit" @click="handleSubmit">快速提交</button>
		</view>
	</view>
</template>

<script>
	import statusBarVue from '@/components/statusBar.vue';

	import {
		postWorksheetCreate
	} from '@/utils/api.js';

	import {
		publics
	} from '@/mixins/publics.js';

	export default {
		mixins: [publics],
		components: {
			statusBarVue
		},
		data() {
			return {
				// 基础表单数据
				baseFormData: {
					price: 0,
					deadline: null,
					atRange:[]
				},
				placeholderStyle: `fontFamily: PingFangSC-Regular;font-weight: 400;font-size: 15px;color: rgba(30, 31, 35, 0.3)`,
				// 校验规则
				rules: {
					serv_addr: {
						rules: [{
							required: true,
							errorMessage: '服务地址不能为空'
						}]
					},
					at: {
						rules: [{
							required: true,
							errorMessage: '服务时间不能为空'
						}]
					},
					service_types: {
						rules: [{
							required: true,
							errorMessage: '需求类型不能为空'
						}]
					},
					content: {
						rules: [{
							required: true,
							errorMessage: '详细描述不能为空'
						}]
					},
					atRange: {
						rules: [{
							required: true,
							errorMessage: '时间不能为空'
						}]
					},
				},
			}
		},
		methods: {
			// 时间范围
			handleDateRange(e){
				this.baseFormData.atRange = e;
			},
			// 提交需求
			handleSubmit() {
				this.$refs.baseForm.validate((err, formData) => {
					if (!err) {
						let postData = formData;
						postData.price = '0'; // 价格
						postData.method = 1; // 服务方式  线下
						postData.serv_domain = 99999; // 需求领域
						postData.title = ''; // 需求标题
						postData.attachments = []; // 附件
						postData.at = this.handleTimeStampToStr(formData.atRange[0]);
						postData.deadline = this.handleTimeStampToStr(formData.atRange[1]);
						postData.serv_addr = formData.serv_addr ? formData.serv_addr.join() : '';
						this.postWorksheetCreateApi(postData);
					}
				});
			},
			postWorksheetCreateApi(postData) {
				postWorksheetCreate(postData).then(res => {
					if (res.code === 200) {
						uni.showModal({
							title: '提示',
							content: '操作成功！',
							success: function(res) {
								if (res.confirm) {
									uni.navigateBack()
								}
							}
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.placeholder,
	.content {
		font-family: PingFangSC-Regular;
		font-weight: 400;
		font-size: 15px;
		color: #A5A5A7;
		margin-right: 10px;

		&.content {
			color: #1E1F23;
		}
	}

	textarea {
		box-sizing: border-box;
		height: 212px !important;
		padding: 6px 12px;
		margin: 0 !important;
	}

	.checklist-tag {
		.checklist-group {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
			justify-content: space-between;
			align-items: center;

			&>.checklist-box.is--tag {
				width: 100px;
				height: 32px;
				line-height: 32px;
				margin-right: 0 !important;
				padding: 0 !important;
				background: #EEF4F8;
				border-radius: 4px;
				border: none !important;

				.checklist-content {
					justify-content: center;
					font-family: PingFangSC-Regular;
					font-weight: 400;
					font-size: 14px;
					color: #2C2C2C;
				}
			}
		}
	}

	.add-demand-page {
		overflow: auto;
		margin: auto;
		position: fixed;
		left: 0;
		right: 0;
		background-color: #F2F3F7;
		padding: 10px 2.66vw;
		box-sizing: border-box;
		z-index: 99;

		.add-demand-page-group {
			border-radius: 10px;
			margin-bottom: 10px;
			overflow: hidden;
			background-color: #fff;

			.price {
				font-family: PingFangSC-Semibold;
				font-weight: 600;
				font-size: 15px;
				color: #F04B40;

				.text {
					margin-right: 10px;
				}
			}

			.checklist-box:last-child {
				margin-right: 4px;
			}

			.uni-data-checklist .checklist-group {
				.checklist-box {
					.checklist-text {
						font-size: 15px;
						color: rgba(0, 0, 0, 0.85);
					}
				}
			}
		}

		.long-width {
			width: 88.27vw;
			margin-top: 8px;

			.file-title {
				font-family: PingFangSC-Regular;
				font-weight: 400;
				font-size: 13px;
				color: rgba(30, 31, 35, 0.4);
			}

			.add-img-icon-group {
				width: 102px;
				height: 102px;
				background: #F5F6FA;
				border-radius: 4px;
				opacity: 0.3;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;

				.add-img-icon {
					width: 26px;
					height: 26px;
				}

				.add-img-text {
					font-family: PingFangSC-Regular;
					font-weight: 400;
					font-size: 13px;
					color: #1E1F23;
				}
			}

			.file-picker__box-content {}
		}

		.uni-forms-item {
			&.is-direction-top {
				.uni-forms-item__label {
					line-height: inherit;
					height: auto;
					padding: 0;
					font-weight: bold;
					color: #1E1F23;
					font-size: 15px;
				}
			}

			&.uni-forms-item--border {
				padding: 0;
				border: none;
			}

			.uni-easyinput__content,
			.is-add {
				background: #F5F6FA !important;
				border-radius: 4px;
			}
		}


		.uni-list {
			background: none;

			.uni-list-item__content-title {
				height: auto;
				font-family: PingFangSC-SNaNpxibold;
				font-weight: bold;
				font-size: 14px;
				color: #606266;
			}

			.list-item {
				.uni-list-item__container {
					height: 35px;
					line-height: 35px;
				}
			}
		}


		.uni-list-item__container {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;

			&-title {
				font-size: 16px;
			}
		}

		.submit {
			width: 100%;
			height: auo;
			margin-top: 10px;

			.btn-submit {
				width: 94.67vw;
				height: 45px;
				background: #22A366;
				border-radius: 8px;
				color: #fff;
				font-weight: 400;
				font-size: 17px;
			}
		}

		.price-popup {
			background: #fff;
			border-radius: 16px 16px 0 0;
			padding: 14px 16px;

			&-h {
				width: 100%;
				height: 27px;
				line-height: 27px;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.tit {
					font-weight: 600;
					font-size: 16px;
					color: #131315;
					height: 50px;
					line-height: 50px;
					display: flex;

					.btn {
						font-weight: 400;
						font-size: 16px;
						color: #161616;
						margin-right: 30px;
						position: relative;

						&.active {
							color: #22A366;
							font-weight: 600;

							&::after {
								display: inline-block;
								content: '';
								width: 16px;
								height: 3px;
								background: #22A366;
								border-radius: 2px;
								position: absolute;
								left: 0;
								right: 0;
								bottom: 8px;
								margin: auto;
							}
						}
					}
				}

				.close {
					width: 16px;
					height: 16px;
					line-height: 16px;
					border-radius: 100%;
					color: #9A9FB3;
					margin: initial;
					padding: 0;
					text-align: center;
				}
			}

			&-b {
				padding: 14px 0;

				.input-group {
					width: 100%;
					height: 42px;
					background: #FFFFFF;
					border: 3px solid #22a36633;
					border-radius: 6px;
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding-right: 18px;
					box-sizing: border-box;

					input {
						width: calc(100% - 30px);
					}

					&.active {
						border: 5px solid #22A366;
					}
				}

				.fixed-price {
					.tit {
						color: #B6B6B6;
						font-size: 14px;
						font-family: PingFangSC-Regular;
						display: flex;
						justify-content: space-between;
						margin-bottom: 4px;

						.describe {
							font-weight: 400;
							color: #22A366;

							.icon-tips {
								display: inline-block;
								width: 14px;
								height: 14px;
								background: url("/static/images/icon/tips.png") no-repeat 0 0;
								background-size: 100% 100%;
							}
						}
					}
				}

				.range {
					.separator {
						width: 10px;
						font-family: PingFangSC-Regular;
						font-weight: 400;
						font-size: 16px;
						color: #B6B6B6;
						text-align: center;
					}

					.tit {
						overflow: hidden;

						&>.label {
							width: 50%;
							color: #B6B6B6;
							font-size: 14px;

							&:last-child {
								width: calc(50% - 10px);
							}

							&:nth-child(1) {
								width: calc(50% + 10px);
							}
						}
					}

					&>.group {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.input-group {
							width: calc(50% - 10px);
						}

					}
				}

				.footer {
					margin-top: 24px;
					overflow: hidden;

					.btn {
						width: 40vw;
						height: 45px;
						border-radius: 8px;

						&::after {
							border: none;
						}

						&-no {
							color: #22A366;
							background-color: #F5F6FA;
						}

						&-yes {
							color: #fff;
							background-color: #22A366;
						}
					}
				}

			}
		}

	}
</style>